package com.xinpu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinpu.entity.TSalesFriends;
import com.xinpu.service.ITSalesFriendsService;
import com.xinpu.service.OwnService;
import com.xinpu.vo.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SalesFriendsController
 *
 * @author ayuan
 * @date 2021/6/4
 */
@RestController
@RequestMapping("/salesFriends")
public class SalesFriendsController {

    @Autowired
    private ITSalesFriendsService itSalesFriendsService;

    /**
     * 获取租户编号
     *
     * @return
     */
    @GetMapping("/getTenantId")
    public ResponseMessage getTenantId() {
        return ResponseMessage.ok(OwnService.getLoginer().getTenantId());
    }


    /**
     * 销售员朋友圈列表
     *
     * @param page
     * @param size
     * @param name
     * @param dateTime
     * @return
     */
    @GetMapping("/list")
    public ResponseMessage list(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "dateTime", required = false) String dateTime
    ) {
        QueryWrapper<TSalesFriends> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("content", name);
        }
        if (StringUtils.isNotBlank(dateTime)) {
            String[] split = dateTime.split(",");
            queryWrapper.between("create_time", split[0], split[1]);
        }
        Page<TSalesFriends> page1 = itSalesFriendsService.page(new Page<>(page, size), queryWrapper);
        return ResponseMessage.ok(page1);
    }

    /**
     * 创建销售员朋友圈列表
     *
     * @param map
     * @return
     */
    @PostMapping("/create")
    public ResponseMessage createOrEdit(@RequestBody Map<String, String> map) {
        String[] sids = map.get("sid").split(",");
        List<TSalesFriends> list = new ArrayList<>();
        for (String sid : sids) {
            TSalesFriends tSalesFriends = new TSalesFriends();
            tSalesFriends.setSid(sid);
            tSalesFriends.setContent(map.get("content"));
            tSalesFriends.setCdr(map.get("cdr"));
            list.add(tSalesFriends);
        }
        itSalesFriendsService.saveBatch(list);
        return ResponseMessage.ok();
    }

    /**
     * 编辑销售员朋友圈
     *
     * @param map
     * @return
     */
    @PostMapping("/edit")
    public ResponseMessage edit(@RequestBody Map<String, String> map) {
        TSalesFriends tSalesFriends = itSalesFriendsService.getById(map.get("id"));
        tSalesFriends.setContent(map.get("content"));
        tSalesFriends.setCdr(map.get("cdr"));
        itSalesFriendsService.updateById(tSalesFriends);
        return ResponseMessage.ok();
    }

    /**
     * 删除销售员朋友圈
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage delete(@RequestBody Map<String, String> map) {
        itSalesFriendsService.removeById(map.get("id"));
        return ResponseMessage.ok();
    }


}
